/*
 * @(#)TestsetAction.java
 *
 *
 *
 * ver 1.0 Sep 6, 2007 ggranum
 */
package org.testfire.realm.view.json.action;

import org.json.JSONException;
import org.testfire.realm.control.exception.UpdateException;
import org.testfire.realm.control.testfire.TestFireDBAO;
import org.testfire.realm.control.user.UserBean;
import org.testfire.realm.model.entities.testfire.TestsetEntity;
import org.testfire.realm.view.json.translator.TestsetTranslator;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class TestsetAction<T> implements JsonAction<String>
{

    TestFireDBAO testfireDBAO;

    UserBean userBean;

	TestsetTranslator testsetTranslator;


    // Inject the lgsrealm entity manager, as defined in the persistence.xml file.
    @PersistenceContext(unitName = "testfire")
    EntityManager em;



    public String perform( String[] argumentKeys, String... args )
        throws UpdateException
    {
	    try
	    {
		    TestsetEntity translatedObj = testsetTranslator.toObject(args[1]);
		    if ( args[0].equals( "commit" ) )
		    {
			    // commit only occurs for objects that exist in the database:
			    TestsetEntity testset = em.find(TestsetEntity.class, translatedObj.getId());
			    testsetTranslator.copyValues(testset, translatedObj);
		        this.testfireDBAO.merge( testset );
		    }
		    return "success";
	    }
	    catch (JSONException e)
	    {
		    throw new RuntimeException(e);
	    }
    }



    public void setTestfireDBAO( TestFireDBAO testfireDBAO )
    {
        this.testfireDBAO = testfireDBAO;
    }

    public void setUserBean( UserBean userBean)
    {
        this.userBean = userBean;
    }

	public void setTestsetTranslator(TestsetTranslator testsetTranslator)
	{
		this.testsetTranslator = testsetTranslator;
	}
}

// eof: TestsetAction.java